﻿#region File Description
//-----------------------------------------------------------------------------
// Program.cs
//
// Author: ITU Team3 (2012)
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;

// Import Service Reference
using rentit03_testClient.rentit03;
#endregion

namespace rentit03_testClient
{
    internal class Program
    {
        private static ServiceClient _service;  // Proxy for Service
        private static string _sesId = "";      // Session id

        private static void Main(string[] args)
        {
            // Create proxy for Service
            _service = new ServiceClient();

            Console.WriteLine("Welcome!");
            Console.WriteLine();
            Console.WriteLine("Valid commands are: 'hello', 'login', 'getdata', 'getmovie', 'logout', 'exit'");
            Console.WriteLine();

            var exit = false;

            // Main loop
            while (!exit)
            {
                Console.Write("> ");
                var cmd = Console.ReadLine();

                switch (cmd)
                {
                    case "exit":
                        exit = true;
                        break;
                    case "login":
                        Login();
                        break;
                    case "logout":
                        Logout();
                        break;
                    case "getdata":
                        GetData();
                        break;
                    case "getmovie":
                        GetMovie();
                        break;
                    case "hello":
                        // Call HelloWorld() on Service.
                        try
                        {
                            Console.WriteLine("Rentit03 says: " + _service.HelloWorld());
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Something went wrong!");
                        }
                        break;
                    default:
                        Console.WriteLine("Unknown command: " + cmd);
                        break;
                }
            }
            _service.Close();
        }

        /// <summary>
        /// Login to the Service. This will start a new session and provide a session id.
        /// </summary>
        private static void Login()
        {
            Console.WriteLine("Please provide username and password (rentit / vandamme)");
            Console.Write("username > ");
            var un = Console.ReadLine();
            Console.Write("password > ");
            var pw = Console.ReadLine();

            try
            {
                _sesId = _service.Login(un, pw);
                Console.WriteLine("You have succesfully logged in!");
                Console.WriteLine("Session id: " + _sesId);
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong! Make sure you have provided the right username and password (rentit / vandamme).");
            }
        }

        /// <summary>
        /// Logout from Service. This will destroy the session.
        /// </summary>
        private static void Logout()
        {
            try
            {
                _service.Logout(_sesId);
                _sesId = "";
                Console.WriteLine("You have succesfully logged out!");
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wront!");
            }
        }

        /// <summary>
        /// Get data from the Servce. This will only work if you are logged in. The method retrieves a string-object for testing purposes.
        /// </summary>
        private static void GetData()
        {
            try
            {
                //var res = _service.GetData(_sesId);
                var res = _service.ReadMedia(1);
                Console.WriteLine("Rentit03 says: " + res);
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong!");
                Console.WriteLine(e.ToString());
            }
        }

        private static void GetMovie()
        {
            Console.Write("Imdb id > ");
            var id = Console.ReadLine();

            try
            {
                var res = _service.GetMovie(_sesId, id);
                Console.WriteLine("JSON:\n\n" + res);
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong! Make sure you are logged in.");
            }
        }
    }
}